package cn.tedu.charging.order.rabbitmq;

import cn.tedu.charging.common.pojo.message.DelayCheckMessage;
import cn.tedu.charging.common.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.common.protocol.WebSocketMessage;
import cn.tedu.charging.order.dao.mapper.ChargingBillFailMapper;
import cn.tedu.charging.order.dao.mapper.ChargingBillSuccessMapper;
import cn.tedu.charging.order.websocket.WebSocketServer;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.xml.ws.handler.LogicalHandler;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class DelayCheckConsumer {
    @Autowired
    private ChargingBillSuccessMapper chargingBillSuccessMapper;
    @Autowired
    private ChargingBillFailMapper chargingBillFailMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WebSocketServer webSocketServer;
    @RabbitListener(queues = "DELAY_BILL_DL_QUEUE")
    public void handleGunCheck(DelayCheckMessage message){
        log.debug("延迟检查消息开始");
        log.debug("执行枪锁逻辑");
        String lockKey="charging:order:"+message.getOrderNo();
       Boolean lock = redisTemplate.opsForValue().setIfAbsent(lockKey,"",5, TimeUnit.MINUTES);
       if (!lock){
           return;
       }
        //使用订单编号查询成功订单和失败订单
       Boolean exists = chargingSuccessOrFail(message.getOrderNo());
       if (!exists){
           cresteFailBill(message);
           WebSocketMessage<String> webSocketMessage = new WebSocketMessage<>();
           webSocketMessage.setState(1);
           webSocketMessage.setMessage("fail");
           webSocketMessage.setData("B 您的订单创建失败,设备有故障,对不起,送您一张优惠券");
           webSocketServer.pushMessage(message.getUserId(), JSON.toJSONString(webSocketMessage));
       }
    }

    private void cresteFailBill(DelayCheckMessage message) {
        ChargingBillFailPO po = new ChargingBillFailPO();
        po.setBillId(message.getOrderNo());
        po.setUserId(message.getUserId());
        //operatorId stationId 远程调用device查询
        po.setGunId(message.getGunId());
        po.setVehicleId(message.getVehicleId());
        po.setDeleted(0);
        po.setCreateTime(new Date());
        po.setUpdateTime(new Date());
        po.setFailDesc("设备自检充电失败");
        chargingBillFailMapper.insert(po);
    }

    private Boolean chargingSuccessOrFail(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id",orderNo);
        Long successCount = chargingBillSuccessMapper.selectCount(queryWrapper);
        Long failCount = chargingBillFailMapper.selectCount(queryWrapper);
        if (successCount!= 0 && failCount!=0){
            return successCount>0 ||  failCount>0;
        }else {
            return false;
        }
    }
}
